//根据数据初始化页面
function initPlugInfo(old_data){
	for(var x=0;x<old_data.length;x++){
		var timingvalue ;
		var plugin = old_data[x].plug;
		if(plugin!=undefined){
			for(var i=0;i<plugin.length; i++){
				var replugName = replace(plugin[i].plugName);
				var paraminfo = plugin[i].param;
				if(paraminfo!=undefined){
					for(var j=0;j<paraminfo.length; j++){
						var paramMap = paraminfo[j];
						var child ;
						for(var key in paramMap){
							var replacekey = replace(key);//把,号换成__
							if(replacekey!="EffectiveType"){
								child= $("#"+replugName).find("#"+replacekey+"_div");//找到最外侧的父亲元素
								child.attr("old_value",paramMap[key]);//获取到初始值
							} 
							var grandson = child.find("#"+replacekey);//找到需要赋值的页面部分
							var grandsonClass = child.attr("class");//获取当前配置块的类型
							var effectiveClass = grandson.attr("class");//是不是图标
						    if(effectiveClass=="img"){
								if(paramMap[key]=="0"){
									grandson.show();
								}
							}else{
								var iscontain =false;
								if(grandsonClass!=undefined){
								iscontain = grandsonClass.indexOf("timer")>-1;	
								}
								if(grandsonClass=="checked_div"){//div
									if(checkIsNeed(grandsonClass,child)){
										checkDiv(child,paramMap[key]);
									}
								}else if(grandsonClass=="collect_port"){//Input	
									if(checkIsNeed(grandsonClass,grandson)){
										grandson.val(paramMap[key]);	
									}
								}else if(grandsonClass=="timer_div"||iscontain){//特定时间格式
									if(checkIsNeed(grandsonClass,child)){
										checkTime(child,paramMap[key]);	
									}
								}else if(grandsonClass=="checked_div_timing"){//特定时间格式
									if(checkIsNeed(grandsonClass,child)){
										timingvalue = paramMap[key];	
									}
								}else if(grandsonClass=="zookeeper_ip"){//IP多配置
									checkIp(child,paramMap[key]);
								}
							}
					 	}
					}
				}
				$("#"+replugName).show();
  			}
			getTimingMode(timingvalue);
	 	}
	}
//	initCheckDivStatus();//初始化div选中与否，每个配置页面自己实现
}
	
//把.换成__
function replace(stringObj){
	var reg=new RegExp(/./g); //创建正则RegExp对象  
	var newstr=stringObj.replace(/\./g,'__');	
	return newstr;
}

//单选框，根据值判断那个框被选中
function checkDiv(child,value){
	child.attr("cur_value",value);
 	var childNodes = child.find(".radio");
   	for(var i=0;i<childNodes.length;i++){
		var val = $(childNodes[i]).attr("value");
		if(val==value){
			$(childNodes[i]).find("ins").attr("class","checked");
		}else{
			$(childNodes[i]).find("ins").attr("class","");	
		}
 	}
}

//时间框赋值，根据时间对时间框进行赋值
function checkTime(child,value){  
	var strs= new Array(); //定义一数组 
	strs=value.split(":"); //字符分割 
    var childhour = child.find("#hour_input");
    var childminute = child.find("#minute_input");
    childhour.val(strs[0]);
    childminute.val(strs[1]);
}

//根据标签类型，确定是否需要进行数据的校验
function checkIsNeed(elementType,obj){
//	if(elementType=="checked_div"||elementType=="timer_div"
//	||elementType=="timer"||elementType=="checked_div_timing"){
//		if(obj.attr("greyTag")==undefined){
//			return true;
//		}else{
//			return false;
//		}
//	}else if(elementType=="collect_port"){
//		if(obj.attr("class").indexOf("normalgrey")<0){
//			return true;
//		}else{
//			return false;
//		}
//	}
	return true;
}

//时间框赋值，根据时间对时间框进行赋值
function checkTime(child,value){  
	var strs= new Array(); //定义一数组 
	strs=value.split(":"); //字符分割 
    var childhour = child.find("#hour_input");
    var childminute = child.find("#minute_input");
    childhour.val(strs[0]);
    childminute.val(strs[1]);
}
//时间框取值
function getcurTime(child){  
	var cur_value;
    var childhour = child.find("#hour_input");
    var childminute = child.find("#minute_input");
    var hour = childhour.val() ;
    var minute = childminute.val();
    cur_value = hour+":"+minute;
    return cur_value;
}
//获取多个IP
function getcurIp(){
	var cur_value="";
	var index = 1;
	$("input[id='DatabaseAPI__HBase__zookeeper_address']").each(function(){
		if(index==1){
			cur_value+=$(this).val();	
		}else{
			cur_value+=","+$(this).val();	
		}
		index=2;
	});
	return cur_value;
}
//获取所有已经改变的数据，
function getAllChangeValue(old_data){
	var cons = "";
	if(old_data!=""){
		var changeValueindex=0;
	    for(var x=0;x<old_data.length;x++){
		    var consObject="";
			var index = 0;
			var valueindex = 0;
			var isvlaueChange = 0;//标识是否有值被改变了 1代表有
			var paramstr ="";
			var plugstr ="";
			consObject += "{";		
			consObject+='"deviceName":'+'"'+old_data[x].deviceName+'",'+
			'"plug": [';
			var plugin=old_data[x].plug;
			if(plugin!=undefined){
				for(var i=0;i<plugin.length; i++){
					valueindex = 0;
					var replugName = replace(plugin[i].plugName);
					var isPluginChange = 0;//当前插件是否有数据更改
					var paraminfo = plugin[i].param;
					paramstr ="";
					if(paraminfo!=undefined){
						for(var j=0;j<paraminfo.length; j++){
							var paramMap = paraminfo[j];
							for(var key in paramMap){
								var replacekey = replace(key);
								if(replacekey!="EffectiveType"){
									var child = $("#"+replugName).find("#"+replacekey+"_div");
									var cur_value ;
									var old_value = child.attr("old_value");
									console.log(replacekey);
									var grandson = child.find("#"+replacekey);
									var grandsonClass = child.attr("class");	
									if(grandsonClass=="checked_div"){//div
										if(checkIsNeed(grandsonClass,child)){
											cur_value = child.attr("cur_value");	
										}else{
											cur_value =undefined;
										}
									}else if(grandsonClass=="collect_port"){//input		
										if(checkIsNeed(grandsonClass,grandson)){
											cur_value = grandson.val();	
										}else{
											cur_value =undefined;
										}
									}else if(grandsonClass=="timer_div"||grandsonClass=="timer"){//时间
										if(checkIsNeed(grandsonClass,child)){
											cur_value = getcurTime(child);	
										}else{
											cur_value =undefined;
										}
									}else if(grandsonClass=="checked_div_timing"){
										if(checkIsNeed(grandsonClass,child)){
											cur_value =getTimingDate();	
										}else{
											cur_value =undefined;
										}
									}else if(grandsonClass=="zookeeper_ip"){
										cur_value =getcurIp();
									}
									if(cur_value!=old_value&&old_value!=undefined&&cur_value!=undefined){
										isvlaueChange = 1;
										isPluginChange =1;
									    if(valueindex==0){
									    	 paramstr+='"'+key+'":'+'"'+cur_value+'"';
									    	 valueindex=2;
									    }else{
									    	 paramstr+=',"'+key+'":'+'"'+cur_value+'"';
									    }
									}
							     }
					        }
					    }
			        }
					if(isPluginChange!=0){
						if(index==0){
							consObject+="{";	
							index=2;
						}else{
							consObject+=",{";	
						}
						consObject+='"plugName":'+'"'+plugin[i].plugName+'",'+
						'"param": {'+
						paramstr+			
						"}"+
						"}";
						
					} 
			  }	
		}
			consObject+=']'+"}";
			if(isvlaueChange==0){
				consObject="";	
			}else{
				changeValueindex++;
			}
			if(consObject!=""){
				if(changeValueindex==1){
					cons+=consObject;
				}else{
					cons+=","+consObject;
				}
			}
	   }
	}
	
  return cons;
}
//获取所有已经改变的数据，
function getAllChangeValueAndOldValue(old_data){
	var cons = "";
	if(old_data!=""){
		var changeValueindex=0;
	    for(var x=0;x<old_data.length;x++){
		    var consObject="";
			var index = 0;
			var valueindex = 0;
			var isvlaueChange = 0;//标识是否有值被改变了 1代表有
			var paramstr ="";
			var plugstr ="";
			consObject += "{";		
			consObject+='"deviceName":'+'"'+old_data[x].deviceName+'",'+
			'"plug": [';
			var plugin=old_data[x].plug;
			if(plugin!=undefined){
				for(var i=0;i<plugin.length; i++){
					valueindex = 0;
					var replugName = replace(plugin[i].plugName);
					var isPluginChange = 0;//当前插件是否有数据更改
					var paraminfo = plugin[i].param;
					paramstr ="";
					if(paraminfo!=undefined){
						for(var j=0;j<paraminfo.length; j++){
							var paramMap = paraminfo[j];
							for(var key in paramMap){
								var replacekey = replace(key);
								if(replacekey!="EffectiveType"){
									var child = $("#"+replugName).find("#"+replacekey+"_div");
									var cur_value ;
									var old_value = child.attr("old_value");
									var changekey = child.attr("springCode");
									var grandson = child.find("#"+replacekey);
									var grandsonClass = child.attr("class");	
									if(grandsonClass=="checked_div"){//div
										if(checkIsNeed(grandsonClass,child)){
											cur_value = child.attr("cur_value");	
										}else{
											cur_value =undefined;
										}
									}else if(grandsonClass=="collect_port"){//input		
										if(checkIsNeed(grandsonClass,grandson)){
											cur_value = grandson.val();	
										}else{
											cur_value =undefined;
										}
									}else if(grandsonClass=="timer_div"||grandsonClass=="timer"){//时间
										if(checkIsNeed(grandsonClass,child)){
											cur_value = getcurTime(child);	
										}else{
											cur_value =undefined;
										}
									}else if(grandsonClass=="checked_div_timing"){
										if(checkIsNeed(grandsonClass,child)){
											cur_value =getTimingDate();	
										}else{
											cur_value =undefined;
										}
									}else if(grandsonClass=="zookeeper_ip"){
										cur_value =getcurIp();
									}
									if(cur_value!=old_value&&old_value!=undefined&&cur_value!=undefined){
										isvlaueChange = 1;
										isPluginChange =1;
									    if(valueindex==0){
									    	 paramstr+='{new_value:'+'"'+cur_value+'",';
									    	 paramstr+='old_value:'+'"'+old_value+'",';
									    	 paramstr+='changeKey:'+'"'+changekey+'"}';
									    	 valueindex=2;
									    }else{
									    	 paramstr+=',{new_value:'+'"'+cur_value+'",';
									    	 paramstr+='old_value:'+'"'+old_value+'",';
									    	 paramstr+='changeKey:'+'"'+changekey+'"}';
									    }
									}
							     }
					        }
					    }
			        }
					if(isPluginChange!=0){
						if(index==0){
							consObject+="{";	
							index=2;
						}else{
							consObject+=",{";	
						}
						consObject+='"plugName":'+'"'+plugin[i].plugName+'",'+
						'"param": ['+
						paramstr+			
						"]"+
						"}";
					} 
			  }	
		}
			consObject+=']'+"}";
			if(isvlaueChange==0){
				consObject="";	
			}else{
				changeValueindex++;
			}
			if(consObject!=""){
				if(changeValueindex==1){
					cons+=consObject;
				}else{
					cons+=","+consObject;
				}
			}
	   }
	}
	
  return cons;
}
//判断是那种定时上传周期模式,并赋值
function getTimingMode(value){
	var timing_mode = $("#CS_InfoCenterPlugin__station_upload_timing_mode_div").attr("old_value");
	if(timing_mode==0){
		$("#month_input").val(value);
		$("#week_arrow_down").addClass("greyStatus").parent().addClass("greyStatus");
		$("#month_arrow_down").removeClass("greyStatus").parent().removeClass("greyStatus");
	}else if(timing_mode==1){
		var str ;
		switch (value) {
		case "1":str = "<spring:message code='Monday' />";
			break;
		case "2":str = "<spring:message code='Tuesday' />";
			break;
		case "3":str = "<spring:message code='Wednesday' />";
			break;
		case "4":str = "<spring:message code='Thirsday' />";
			break;
		case "5":str = "<spring:message code='Friday' />";
			break;
		case "6":str = "<spring:message code='Saturday' />";
			break;
		case "7":str = "<spring:message code='Sunday' />";
			break;
		default:str= "<spring:message code='Monday' />";
			break;
		}
		$("#week_input").val(str);
		$("#week_arrow_down").removeClass("greyStatus").parent().removeClass("greyStatus");
		$("#month_arrow_down").addClass("greyStatus").parent().addClass("greyStatus");
	}else if(timing_mode==2){
		$("#month_arrow_down").addClass("greyStatus").parent().addClass("greyStatus");
		$("#week_arrow_down").addClass("greyStatus").parent().addClass("greyStatus");
	}
	$("#CS_InfoCenterPlugin__station_upload_timing_date_div").attr("cur_value",value);
}
function check(obj) {
    var value = $(obj).attr("value");
    $(obj).parents(".checked_div").attr("cur_value",value);
}